<?php
/**
 * Created by PhpStorm.
 * User: 沁塵
 * Date: 2019/4/20
 * Time: 19:57
 */

namespace app\api\controller\v1;

use app\api\model\ProductConfig;
use app\api\model\ProductIntroduceZimu;
use app\api\model\ProductScript;
use app\api\model\TempKong;
use app\api\model\TempKongVideo;
use think\Request;

class Temp
{
    public function getKongEvent()
    {
        $info = TempKong::where("status", 1)->findOrEmpty();
        if (!$info->isEmpty()) {
            $info->status = 2;
            $info->save();
            return writeJson(200,$info,'完成！');
        } else {
            return writeJson(200,[],'完成！');
        }
    }

    public function backfillKongEvent(Request $request)
    {
        $params = $request->post();
        $info = TempKong::where("id", $params["id"])->findOrEmpty();
        if (!$info->isEmpty()) {
            $info->status = $params["status"];
            $info->save();
            return writeJson(200,$info,'成功！');
        } else {
            return writeJson(400,[],'找不到！');
        }
    }

    public function getZimuEvent()
    {
        $info = ProductIntroduceZimu::where("status", 1)->findOrEmpty();
        if (!$info->isEmpty()) {
            $info->status = 2;
            $info->save();
            return writeJson(200,$info,'完成！');
        } else {
            return writeJson(200,[],'完成！');
        }
    }

    public function backfillZimuEvent(Request $request)
    {
        $params = $request->post();
        $info = ProductIntroduceZimu::where("id", $params["id"])->findOrEmpty();
        if (!$info->isEmpty()) {
            $info->status = $params["status"];
            $info->zimu = $params["zimu"];
            $info->save();
            return writeJson(200,$info,'成功！');
        } else {
            return writeJson(400,[],'找不到！');
        }
    }

    public function addKongVideo(Request $request) {
        $params = $request->post();
        TempKongVideo::create($params);
        return writeJson(200, '', '请求成功');
    }

    public function checkKongVideo(Request $request) {
        $params = $request->post();
        $event = new TempKongVideo();
        $count = $event->where("model", $params["model"])
            ->where("filename", $params["filename"])
            ->count();
        if ($count == 0) {
            return writeJson(200, false, '请求成功');
        } else {
            return writeJson(200, true, '请求成功');
        }
    }

    public function getKongVideo(Request $request)
    {
        $params = $request->post();
        $info = TempKongVideo::where("model", $params["model"])
            ->orderRand()->limit(5)->select()->toArray();
        if (!empty($info)) {
            return writeJson(200, $info, '请求成功');
        } else {
            return writeJson(200, [], '无');
        }
    }

    public function getProductConfig(Request $request)
    {
        $params = $request->get();
        $info = ProductConfig::where("pid", $params["pid"])->findOrEmpty();
        if (!$info->isEmpty()) {
            return writeJson(200, $info, '请求成功');
        } else {
            return writeJson(200, [], '无');
        }
    }

//    public function setProductConfig(Request $request)
//    {
//        $params = $request->post();
//        $info = ProductConfig::where("pid", $params["pid"])->findOrEmpty();
//        if (!$info->isEmpty()) {
//            if (isset($params["script1"])) {
//                $info->where("id", $info["id"])->update(["script1"=>$params["script1"]]);
//            }
//            if (isset($params["script2"])) {
//                $info->where("id", $info["id"])->update(["script2"=>$params["script2"]]);
//            }
//            return writeJson(200, $info, '请求成功');
//        } else {
//            ProductConfig::create($params);
//            return writeJson(200, [], '无');
//        }
//    }

    public function setProductScript(Request $request)
    {
        $params = $request->post();
        $info = ProductScript::where("pid", $params["pid"])->where("model", $params["model"])
            ->where("script_name", $params["script_name"])->findOrEmpty();
        if (!$info->isEmpty()) {
            $info->where("id", $info["id"])->update(["script"=>$params["script"]]);
        } else {
            ProductScript::create($params);
        }
        return writeJson(200, true, '请求成功');

    }

    public function getProductScript(Request $request)
    {
        $params = $request->get();
        $info = ProductScript::where("pid", $params["pid"])->where("model", $params["model"])
            ->where("script_name", $params["script_name"])->findOrEmpty();
        if (!$info->isEmpty()) {
            return writeJson(200, $info, '请求成功');
        } else {
            return writeJson(400, [], '无');
        }
    }
}